#include "main.h"
//创建一个节点
Pflight INIT_Flight(void){
	Pflight f = (Pflight)malloc(sizeof(Flight));
	if(NULL==f)
	{
		perror("malloc");
		return NULL;
	}
	strcpy(f->number,"xxxxxx");
	strcpy(f->staddress,"xxxxxx");
	strcpy(f->arraddress,"xxxxxx");
	strcpy(f->DATE,"xxxxxx");
	strcpy(f->TYPE,"xxxxxx");
	f->stime=0;
	f->atime=0;
	f->value=0;
	INIT_LIST_HEAD(&f->list);
	return f;
}
void Enum(void){
printf("******************\n");
printf("**\033[1;34m欢迎来到XX航空\033[0m**\n");
printf("******************\n");
}
void INput_ACCESs(void){
	int choice_num;
    printf("1.添加航班  2.删除航班  3.查询航班\n");
    printf("请输入你的选择:");
}
void INPUT_MESSAGE(Pflight head){
		Pflight p =INIT_Flight();
		int aaa=0;
		printf("%s\n",p->number);
		printf("请输入航班号     格式为:两个字母加四个数字 例如:AX1235\n");
		scanf("%s",p->number);
		printf("请输入起点站     格式为:城市的拼音        例如:shenzhen\n");
		scanf("%s",p->staddress);
		do{
		printf("请输入终点站(终点站不可与起点站相同)     格式为:城市的拼音        例如:beijing\n");
		scanf("%s",p->arraddress);
		}while(strcmp(p->arraddress,p->staddress)==0);
		//do{ }while(strncmp(p->arraddress,p->staddress,18)==0 && strlen(p->arraddress)!=strlen(p->staddress));
		printf("请输入航班起飞日期   格式为:城市的拼音        例如:2024-3-10\n");
		char xx1[30];
		scanf("%s",xx1);
		strcpy(p->DATE,xx1);
		// char* z=strtok(xx1, "-");
		// char*y = strtok(NULL, "-");
		// char* x = strtok(NULL, "-");
		// sscanf(xx1, "%d-%d-%d",z,y,x);
		// char stime[20]=head->DATE;
		// char* z=strtok(stime, "-");
		// char*y = strtok(NULL, "-");
		// char* x = strtok(NULL, "-");
		// int z1=atoi(z),y1=atoi(y),x1=atoi(x);
		printf("请输入机型      格式为:两个字母加三个数字        例如:by737\n");
		scanf("%s",p->TYPE);
		printf("请输入起飞时间  格式为:小时:分钟        例如:14:20\n");
		char xx[30];
		scanf("%s",xx);
		int o, l;
		sscanf(xx, "%d:%d", &o, &l);
		p->stime=o*60+l;
		printf("请输入到达时间  格式为:小时:分钟        例如:14:20\n");
		char yy[30];
		scanf("%s",yy);
		int o1, l1;
		sscanf(yy, "%d:%d", &o1, &l1);
		p->atime=o1*60+l1;
		printf("请输入票价     格式为:纯数字        例如:3000\n");
		scanf("%d",&p->value);
		list_add(&p->list, &head->list);
	}
	 void DEl_MESSAGE(Pflight Fhead,char *p){
		    struct list_head *pos = NULL;
    		Pflight node = NULL;
			list_for_each(pos, &Fhead->list);
   			 {
        		node = list_entry(pos,typeof(*node),list);    
        		if (strcmp(p,node->number)==0)
        		{
            	pos = pos->prev;  
            	list_del_init(&node->list);
            	free(node);
        		}
    		}
	}
	void Find_MESSAGE(Pflight Fhead){
			int a=0,c=0,p=0;
			char name1[18];
			do{
			printf("请输入要查询的信息：\n");
			printf("1.航班号 2.起点站 3.终点站 4.起飞时间 5.机型 6.票价 输入其他数字请重新输入\n");
			scanf("%d",&a);
			switch(a){
				case 1:
					printf("请输入航班名");
					scanf("%s",name1);
				FIND_FLIGHT_Name(Fhead,name1,strlen(Fhead->number));
					break;
				case 2:
				printf("请输入起点站\n");
					scanf("%s",name1);
				FIND_FLIGHT_Staddress(Fhead,name1,strlen(Fhead->staddress));
					break;
				case 3:
				printf("请输入终点站\n");
					scanf("%s",name1);
				FIND_FLIGHT_Arraddress(Fhead,name1,strlen(Fhead->arraddress));
					break;
				case 4:
					printf("暂未开发此功能");
					break;
				case 5:
				printf("请输入机型\n");
					scanf("%s",name1);
				FIND_FLIGHT_TYPE(Fhead,name1,strlen(Fhead->TYPE));
					break;
				case 6:
				printf("请输入需要查询的方式:1.确定票价查找 2.票价按升序排列\n");
					scanf("%d",&p);
					if(p==1)
				FIND_FLIGHT_CERTAIN_MONEY(Fhead,c,Fhead->value);
				    else if(p==2)
				FIND_FLIGHT_EVERYMONEY(Fhead);
					break;
				default:
					printf("请重新输入\n");
				break;
			}
			}while(a);
	}
	void FIND_FLIGHT_Name(Pflight Fhead,char p[],int b){
	struct list_head *pos = NULL;
    Pflight node = NULL;
	int i;
    list_for_each(pos, &Fhead->list)
    {
        node = list_entry(pos, typeof(*Fhead), list);  
        	if (strncmp(p,node->number,b)==0){
				FInd(node);
				break;
			}
    	}
	}
	void FInd(Pflight node){
			printf("该航班航班号为：  %s\n",node->number);
			printf("该航班起点站为：  %s\n",node->staddress);
			printf("该航班终点站为：  %s\n",node->arraddress);
			printf("该航班起飞日期：  %s\n",node->DATE);
			printf("该航班类型为：    %s\n",node->TYPE);
			printf("该航班起飞时间为： %02d:%02d\n",node->stime/60,node->stime%60);
			printf("改航班到达时间为： %02d:%02d\n",node->atime/60,node->atime%60);
			printf("%d\n",node->value);
	}
	void FIND_FLIGHT_Staddress(Pflight Fhead,char p[],int b){
	struct list_head *pos = NULL;
    Pflight node = NULL;
	int i;
    list_for_each(pos, &Fhead->list)
    {
        node = list_entry(pos, typeof(*Fhead), list);  
        	if (strncmp(p,node->staddress,b)==0){
				FInd(node);
				break;
			}
    	}
	}
	void FIND_FLIGHT_Arraddress(Pflight Fhead,char p[],int b){
	struct list_head *pos = NULL;
    Pflight node = NULL;
	int i;
    list_for_each(pos, &Fhead->list)
    {
        node = list_entry(pos, typeof(*Fhead), list);  
        	if (strncmp(p,node->arraddress,b)==0){
				FInd(node);
				break;
			}
    	}
	}
	void FIND_FLIGHT_TYPE(Pflight Fhead,char p[],int b){
	struct list_head *pos = NULL;
    Pflight node = NULL;
	int i;
    list_for_each(pos, &Fhead->list)
    {
        node = list_entry(pos, typeof(*Fhead), list);  
        	if (strncmp(p,node->TYPE,b)==0){
				FInd(node);
				break;
			}
    	}
	}
	void FIND_FLIGHT_CERTAIN_MONEY(Pflight Fhead,int value,int b){
		struct list_head *pos = NULL;
		Pflight node = NULL;
		int i;
		list_for_each(pos, &Fhead->list)
    {
        node = list_entry(pos, typeof(*Fhead), list);  
        	if (value==b){
				FInd(node);
				break;
			}
    	}
	}
	void FIND_FLIGHT_EVERYMONEY(Pflight Fhead){
		printf("hksadhasjdk\n");
		struct list_head *PP;
		Pflight node1 = NULL;//
		Pflight PP1 = Fhead;
		
		Pflight POS1=NULL;
		struct list_head *z2=&Fhead->list;

		struct list_head *z =(&Fhead->list)->next;
		Pflight z1 =list_entry(z, typeof(Flight),list);
		struct list_head *node;
		(&Fhead->list)->next=&Fhead->list;
		(&Fhead->list)->prev=&Fhead->list;
		struct list_head *pos1=&Fhead->list;
		struct list_head*zos;
		zos=pos1;
		struct list_head*cos;


		while(z!=&Fhead->list){
			node=z;
			POS1=list_entry(node, typeof(Flight),list);
			z=z->next;
			PP1=list_entry(z, typeof(Flight),list);
			while(zos->next!=pos1){
				if(PP1->value<POS1->value){
						zos=zos->next;
				}
				else
					break;
			}
			list_add(node,zos);
			zos=&Fhead->list;
		}
display(Fhead);
}
	void display(Pflight Fhead){
		Pflight POS=NULL;
		list_for_each_entry(POS,&Fhead->list,list){
				FInd(POS);
		}
	}